﻿using System;
using NPMonitor.Implementation;

namespace NPMonitor.DataModel
{
    public static class ModelExtensions
    {
        /// <summary>
        /// 根据机器名获取Active Job的数量
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="machineName"></param>
        /// <returns></returns>
        public static int ActiveJobCount(this MachineData obj, string machineName)
        {
            return StoreManager.Current.JobStore.GetActiveScheduleJobsTotalCountByMachineName(machineName);
        }

        /// <summary>
        /// 根据task id，获取job name 或 workflow name
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public static string GetNameByTaskId(this JobData obj, int taskId, string taskType)
        {
            TaskType type = (TaskType)Enum.Parse(typeof(TaskType), taskType);
            if (type == TaskType.Job)
            {
                JobDefinition jobInfo = StoreManager.Current.JobDefinitionStore.GetJobDefinitionById(taskId);
                if (jobInfo != null)
                    return jobInfo.JobName;
                else
                    return "";
            }
            else if (type == TaskType.Workflow)
            {
                WorkflowData workflowInfo = StoreManager.Current.WorkflowStore.GetWorkflowDataById(taskId);
                if (workflowInfo != null)
                    return workflowInfo.WorkflowName;
                else
                    return "";
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 根据job id，获取job name
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public static string GetJobNameByJobId(this NotificationData obj, int jobId)
        {
            JobDefinition jobInfo = StoreManager.Current.JobDefinitionStore.GetJobDefinitionById(jobId);

            if (jobInfo != null)
                return jobInfo.JobName;
            else
                return "";
        }

        public static string GetJobName(this JobExecutionHistory obj, int jobId)
        {
            JobDefinition jobInfo = StoreManager.Current.JobDefinitionStore.GetJobDefinitionById(jobId);
            if (jobInfo != null)
                return jobInfo.JobName;
            else
                return "";
        }

        public static string GetWorkflowName(this JobExecutionHistory obj, int workflowId)
        {
            var workflow = StoreManager.Current.WorkflowStore.GetWorkflowDataById(workflowId);
            return workflow == null ? string.Empty : workflow.WorkflowName;
        }
        
        /// <summary>
        /// Get Pool Name By Pool Id
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="poolId"></param>
        /// <returns></returns>
        public static string GetPoolNameByPoolId(this MachineData obj, int poolId)
        {
            var pool = StoreManager.Current.MachineStore.GetMachinePoolInfoById(poolId);
            return null != pool ? pool.PoolName : null;
        }

        public static string GetPoolNameByMachineName(this JobData obj, string machineName)
        {
            MachineData machineInfo = StoreManager.Current.MachineStore.GetMachineByMachineName(machineName);
            return null != machineName ? machineInfo.PoolName : null;
        }

        public static string GetMachineStatusByMachineName(this JobData obj, string machineName)
        {
            MachineData machineInfo = StoreManager.Current.MachineStore.GetMachineByMachineName(machineName);
            return machineInfo.Connectivity;
        }
    }
}
